package io.terminus.lego.shinda.web.config

import io.terminus.lego.shinda.web.filter.BindContextFilter
import io.terminus.lego.shinda.web.filter.UserContextFilter
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.web.servlet.FilterRegistrationBean
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

/**
 * WebConfig 注册filter，并配置Order，注意有些filter的Order对于其作用有很大影响
 * @author wangmeng
 * @date 2018/4/16
 */
@Configuration
class WebConfig @Autowired constructor(val bingContextFilter: BindContextFilter,
                                       val userContextFilter: UserContextFilter) {

    @Bean
    fun bindingFilterRegistrationBean(): FilterRegistrationBean<BindContextFilter> {
        val registration = FilterRegistrationBean<BindContextFilter>()
        registration.filter = bingContextFilter
        registration.order = 100
        registration.setName(bingContextFilter.javaClass.name)
        registration.addUrlPatterns("/*")
        return registration
    }

    @Bean
    fun userContextFilterRegistrationBean(): FilterRegistrationBean<UserContextFilter> {
        val registration = FilterRegistrationBean<UserContextFilter>()
        registration.filter = userContextFilter
        registration.order = 100
        registration.setName(userContextFilter.javaClass.name)
        registration.addUrlPatterns("/*")
        return registration
    }
}